package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.itemCatMapper;
import com.jt.pojo.ItemCat;
import net.sf.jsqlparser.expression.operators.relational.ItemsList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class itemCatServiceImpl implements itemCatService {
    @Autowired
    private itemCatMapper itemCatMapper;
    private QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<ItemCat>();

    public ArrayList<Integer> deleteLevel(ItemCat itemCat) {
        ArrayList<Integer> itemList = new ArrayList<>();
//        Integer[] itemlist={};
        queryWrapper.clear();
        queryWrapper.eq("parent_id", itemCat.getId());
        List<ItemCat> list = itemCatMapper.selectList(queryWrapper);
        if (list.isEmpty())return itemList;
        System.out.println("三级目录id:" + list);

        for (ItemCat item : list) {
            itemList.add(item.getId());
        }
        return itemList;
    }

    public void deleteLevel2(ItemCat itemCat) {
        ArrayList<Integer> itemList = deleteLevel(itemCat);
        itemList.add(itemCat.getId());
        System.out.println("二三级目录id:" + itemList);
        queryWrapper.clear();
        queryWrapper.in("id",itemList);
        itemCatMapper.delete(queryWrapper);
    }

    public void deleteLevel1(ItemCat itemCat) {
        ArrayList<Integer> list = deleteLevel(itemCat);//二级目录数组
        if (list.isEmpty()){
            itemCatMapper.deleteById(itemCat.getId());
        }else {
            System.out.println(list);
            queryWrapper.clear();
            queryWrapper.in("parent_id", list).select("id");
            List<ItemCat> list1 = itemCatMapper.selectList(queryWrapper);
            for (ItemCat item : list1) {
                list.add(item.getId());//添加三级目录id到list中
            }
            list.add(itemCat.getId());//添加一级目录到list中
            queryWrapper.clear();
            queryWrapper.in("id",list);
            itemCatMapper.delete(queryWrapper);
        }
    }

//    private void deletelevel2(ItemCat itemCat){
//        queryWrapper.clear();
//        queryWrapper.eq("parent_id", itemCat.getId());
//        itemCatMapper.delete(queryWrapper);
//        itemCatMapper.deleteById(itemCat.getId());
//    }

    @Override
    @Transactional
    public void deleteItemCat(ItemCat itemCat) {
        if (itemCat.getLevel() == 3) {
            itemCatMapper.deleteById(itemCat.getId());
            return;
        }
        if (itemCat.getLevel() == 2) {
            deleteLevel2(itemCat);
            return;
//            deletelevel2(itemCat);
        }
        if (itemCat.getLevel() == 1) {
            deleteLevel1(itemCat);
//            queryWrapper.clear();
//            queryWrapper.eq("parent_id", itemCat.getId());
//            List<ItemCat> itemCatList = itemCatMapper.selectList(queryWrapper);//第一列信息
//            System.out.println(itemCatList);
//            for (ItemCat twoId : itemCatList) {
//                deletelevel2(twoId);
//            }
//            itemCatMapper.deleteById(itemCat.getId());
        }

    }

    @Override
    @Transactional
    public void updateItemCat(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    @Override
    @Transactional
    public void saveItemCat(ItemCat itemCat) {
        itemCat.setStatus(true);
        itemCatMapper.insert(itemCat);
    }

    @Override
    @Transactional
    public void updateItemStatus(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    public Map<Integer, List<ItemCat>> getMap() {
        long start = System.currentTimeMillis();
        HashMap<Integer, List<ItemCat>> map = new HashMap<>();
        List<ItemCat> list = itemCatMapper.selectList(null);
        for (ItemCat itemCat : list) {
            //如果map中有key，那么找到父级的子级序列 然后添加子序列到父序列中
            //如果map中没有key，那么当前序列就是父级序列,然后添加到map中
            if (map.containsKey(itemCat.getParentId())) {
                map.get(itemCat.getParentId()).add(itemCat);//根据key(父序列id)找到value，然后把对象添加到value中
            } else {
                //如果没有父级
                ArrayList<ItemCat> initList = new ArrayList<>();//创建新的list集合
                initList.add(itemCat);//把数据添加到list中
                map.put(itemCat.getParentId(), initList);//把父级id作为key，对象作为value存入map中
            }
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start + "毫秒");
        return map;
    }

    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        Map<Integer, List<ItemCat>> map = getMap();
        if (level == 1) {
            return map.get(0); //所有parentId为0的都是父级 level都为1
        }
        if (level == 2) {
            return getLevel2(map);
        }
        return getLevel3(map);
    }

    private List<ItemCat> getLevel2(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> list = map.get(0);//第一级 (K,V)(0,[{...,..,..},{..,..,..},{..,..,..}])
//            System.out.println(list);
        for (ItemCat itemCat : list) {//遍历level1中的子序列对象集合(ArrayList<ItemCat>)
//            System.out.println(itemCat);
            List<ItemCat> twoList = map.get(itemCat.getId());//获取Parentid对应的子级序列
//            System.out.println(twoList);
            itemCat.setChildren(twoList);
        }
        return list;
    }

    private List<ItemCat> getLevel3(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> list = getLevel2(map);//获取二级序列
        for (ItemCat itemCat : list) {//遍历二级序列
//            System.out.println(list);
//            System.out.println(itemCat);
            List<ItemCat> list2 = itemCat.getChildren();//找到二级序列
            if (list2==null||list2.size()==0)continue;
            for (ItemCat itemCat2 : list2) {
                List<ItemCat> list3 = map.get(itemCat2.getId());
                itemCat2.setChildren(list3);
            }
            itemCat.setChildren(list2);
        }
        return list;
    }
}
